package com.heima.article.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.atricle.dtos.ArticleDto;
import com.heima.model.atricle.pojos.ApArticle;
import com.heima.model.atricle.pojos.ApArticleConfig;
import com.heima.model.atricle.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApAuthorMapper apAuthorMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //1.判断参数 老套路啦
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2. 复制属性
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        //3.判断文章是否存在
        //3.1 如果文章不存在 执行保存
        if (apArticle.getId() == null || apArticle.getId() == 0) {
            //处理作者ID的保存

            save(apArticle);
            //保存ap_article_config
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setIsComment(true); //允许评论
            apArticleConfig.setIsForward(true); //允许转发
            apArticleConfig.setIsDelete(false); //不允许删除
            apArticleConfig.setIsDown(false); //不允许删除
            apArticleConfig.setArticleId(apArticle.getId()); //文章ID
            apArticleConfigMapper.insert(apArticleConfig);
            //保存ap_article_content
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setContent(dto.getContent());
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContentMapper.insert(apArticleContent);



        }            else{  //3.2如果id存在更新文章
            ApArticle article = getById(apArticle.getId());
            if(apArticle==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);

            }
            //更新ap_article_content
            ApArticleContent ApArticleContentUpdate = new ApArticleContent(); //构建要更新的数据
            ApArticleContentUpdate.setContent(dto.getContent());
            apArticleContentMapper.update(ApArticleContentUpdate, Wrappers.<ApArticleContent>lambdaUpdate().eq(ApArticleContent::getArticleId, apArticle.getId()));

        }


        return ResponseResult.okResult(apArticle.getAuthorId());


    }
}